#ifndef SGU_BSTORAGE_H
#define SGU_BSTORAGE_H

SGU_NS_BEGIN

class BIStorage : public NoCopy, public UsedObj
{
	public:
	BIStorage(const ConstSegment &);
	virtual ~BIStorage();

	enum Cmd {BEGIN,END,ABORT};
	
	BIStorage & operator >> (Serializable &);
	BIStorage & operator >> (Cmd);

	BIStorage & operator >> (char &);
	BIStorage & operator >> (unsigned char &);
	BIStorage & operator >> (short &);
	BIStorage & operator >> (unsigned short &);
	BIStorage & operator >> (int &);
	BIStorage & operator >> (unsigned int &);
	BIStorage & operator >> (long &);
	BIStorage & operator >> (unsigned long &);

	BIStorage & operator >> (double &);
	BIStorage & operator >> (bool &);
	BIStorage & operator >> (std::string &);
	BIStorage & operator >> (std::wstring &);

	void checkBitFormat();
	
	protected:
	void read(void *, size_t);
	void skip(size_t);
	const char * queryPtr() const;

	private:
	const ConstSegment & _data;
	size_t _ptr;
	std::stack<size_t> _blocks;
};


class BOStorage : public NoCopy
{
	public:
	BOStorage(DynamicSegment &);
	virtual ~BOStorage();

	enum Cmd {BEGIN,END};

	BOStorage & operator << (const Serializable &);
	BOStorage & operator << (Cmd);

	BOStorage & operator << (char);
	BOStorage & operator << (unsigned char);
	BOStorage & operator << (short);
	BOStorage & operator << (unsigned short);
	BOStorage & operator << (int);
	BOStorage & operator << (unsigned int);
	BOStorage & operator << (long);
	BOStorage & operator << (unsigned long);	
	
	BOStorage & operator << (double);
	BOStorage & operator << (bool);
	BOStorage & operator << (const std::string &);
	BOStorage & operator << (const std::wstring &);

	void writeBitFormat();
	
	protected:
	void write(const void *, size_t);
	
	private:
	DynamicSegment & _data;
	size_t _ptr;
	std::stack<size_t> _blocks;
};


SGU_NS_END

#endif
